import 'dart:async';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_news/common/router/router.dart';
import 'package:flutter_news/common/utils/utils.dart';
import 'package:flutter_news/common/values/values.dart';
import 'package:flutter_news/global.dart';

/// 检查是否有 token
Future<bool> isAuthenticated() async {
  var profileJSON = StorageUtil().getJson(STORAGE_USER_PROFILE_KEY);
  return profileJSON != null ? true : false;
}

/// 删除缓存 token
Future deleteAuthentication() async {
  await StorageUtil().remove(STORAGE_USER_PROFILE_KEY);
  Global.profile = null;
}

/// 重新登录
Future goLoginPage(BuildContext context) async {
  await deleteAuthentication();
  // Navigator.pushNamedAndRemoveUntil(
  //     context, "/sign-in", (Route<dynamic> route) => false);
  // 这个方法跟上面的方法功能一样，可以退出所有的栈，在启动目标 activity
  context.router.pushAndPopUntil(SignInRoute(), predicate: (Route<dynamic> route) {
    return false;
  });
}
